<template>
    <div class="container">
        <el-row>
            <el-breadcrumb separator="/">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>咨询管理</el-breadcrumb-item>
                <el-breadcrumb-item>咨询预约管理</el-breadcrumb-item>
            </el-breadcrumb>
        </el-row>
        <el-row>
            <el-form :inline="true" :model="searchAppointment" class="demo-form-inline">
                <el-form-item label="咨询状态:">
                    <el-radio-group @input="queryList" v-model="searchAppointment.status" size="small">
                        <el-radio-button :label="null">全部</el-radio-button>
                        <el-radio-button :label="0">未回复</el-radio-button>
                        <el-radio-button :label="1">已回复</el-radio-button>
                        <el-radio-button :label="2">未评价</el-radio-button>
                        <el-radio-button :label="3">已评价</el-radio-button>
                    </el-radio-group>
                </el-form-item>
                &nbsp;
                <el-form-item label="咨询类型:">
                    <el-select @clear="queryList" size="small" v-model="searchAppointment.typeId" filterable
                        @change="queryList" clearable placeholder="请选择咨询类型">
                        <el-option v-for="item in typeList" :key="item.id" :label="item.name" :value="item.id">
                        </el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="创建日期:">
                    <el-date-picker size="small" clearable value-format="yyyy-MM-dd" v-model="dateList" type="daterange"
                        range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期">
                    </el-date-picker>
                </el-form-item>
            </el-form>
        </el-row>
        <el-row>
            <el-table tooltip-effect="light" :cell-style="{ 'text-align': 'center' }"
                :header-cell-style="{ 'text-align': 'center' }" :data="appointmentList" border style="width: 100%">
                <el-table-column width="80" prop="id" label="ID">
                </el-table-column>
                <el-table-column prop="user.username" label="咨询用户">
                </el-table-column>
                <el-table-column prop="consult.name" label="咨询师">
                </el-table-column>
                <el-table-column :show-overflow-tooltip="true" prop="content" label="咨询内容">
                </el-table-column>
                <el-table-column label="咨询类型" width="120">
                    <template slot-scope="scope">
                        <el-tag type="warning">{{ scope.row.specialty.name }}</el-tag>
                    </template>
                </el-table-column>
                <el-table-column prop="createTime" width="100" label="创建时间">
                </el-table-column>
                <el-table-column :show-overflow-tooltip="true" label="回复内容">
                    <template slot-scope="scope">
                        <p v-html="scope.row.reply"></p>
                    </template>
                </el-table-column>
                <el-table-column prop="replyTime" width="100" label="回复时间">
                </el-table-column>
                <el-table-column label="状态">
                    <template slot-scope="scope">
                        <el-tag v-if="scope.row.status == 0" type="info">未回复</el-tag>
                        <el-tag v-if="scope.row.status == 1">已回复</el-tag>
                        <el-tag v-if="scope.row.status == 2" type="warning">未评价</el-tag>
                        <el-tag v-if="scope.row.status == 3" type="success">已评价</el-tag>
                    </template>
                </el-table-column>
                <el-table-column fixed="right" label="操作" width="200">
                    <template slot-scope="scope">
                        <!-- :disabled="scope.row.status > 0 ? true : false" -->
                        <el-button @click="clickEdit(scope.row)" icon="el-icon-edit" type="primary"
                            size="mini">{{ scope.row.status > 0 ? '修改回复' : '回复' }}</el-button>
                        <el-button type="danger" icon="el-icon-delete" @click="deleteAppointment(scope.row.id)"
                            size="mini">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
        </el-row>
        <el-row>
            <el-pagination background @size-change="handleSizeChange" @current-change="handleCurrentChange"
                :current-page="searchAppointment.pageNum" :page-sizes="[1, 5, 10, 20]"
                :page-size="searchAppointment.pageSize" layout="total, sizes, prev, pager, next, jumper" :total="total">
            </el-pagination>
        </el-row>
        <el-row>
            <el-dialog @close="clearData" :title="'回复'" :visible.sync="dialogFormVisible">
                <el-form :model="appointment" ref="appointmentForm">
                    <el-form-item label="咨询内容" :label-width="'70px'">
                        <el-input autosize type="textarea" readonly resize="none" :value="appointment.content"></el-input>
                    </el-form-item>
                    <el-form-item label="回复内容" :label-width="'70px'">
                        <quill-editor :options="editorOption" v-model="appointment.reply">

                        </quill-editor>
                    </el-form-item>

                    <el-form-item prop="consultId" label="咨询师:" :rules="[{
                        required: true, message: '请选择咨询师', type: 'number',
                        trigger: 'blur'
                    }]" label-width="70px">
                        <el-select v-model.number="appointment.consultId" filterable placeholder="请选择咨询师">
                            <el-option v-for="item in consultList" :key="item.id" :label="item.name" :value="item.id">
                            </el-option>
                        </el-select>
                    </el-form-item>
                </el-form>
                <div slot="footer" class="dialog-footer">
                    <el-button @click="clearData">取 消</el-button>
                    <!-- <el-button v-if="isadd" type="primary" @click="add">添 加</el-button> -->
                    <el-button type="primary" @click="edit">{{ appointment.status > 0 ?
                        '修 改' : '回 复' }}</el-button>
                </div>
            </el-dialog>
        </el-row>
    </div>
</template>

<script>
import { url } from '@/util/tools';
export default {
    data() {
        return {
            appointmentList: [],
            baseUrl: url,
            searchAppointment: {
                start: null,
                end: null,
                status: null,
                typeId: null,
                pageNum: 1,
                pageSize: 10,
            },
            total: 0,
            dialogFormVisible: false,
            appointment: {
                id: null,
                typeId: null,
                content: '',
                consultId: null,
                reply: '',
                status: 0
            },
            //咨询类型
            typeList: [],
            dateList: [],
            //文本编辑器工具栏
            editorOption: {
                modules: {
                    toolbar: [
                        ['bold', 'italic', 'underline', 'strike'],        // toggled buttons
                        ['blockquote', 'code-block']
                    ]
                },
                placeholder: '输入回复内容'
            },
            //咨询师列表
            consultList: [],
        }
    },
    methods: {
        // queryList() {

        // },
        //点击编辑
        clickEdit(row) {
            this.appointment = row
            this.dialogFormVisible = true
        },
        //回复信息
        edit() {
            this.$refs.appointmentForm.validate((valid) => {
                if (valid) {
                    this.appointment.status = 1
                    this.$http.put('appointment', this.appointment)
                        .then(res => {
                            if (res.data.status == 200) {
                                this.queryList()
                                this.clearData()
                                this.$message({
                                    message: '修改成功',
                                    type: 'success'
                                })
                            } else {
                                this.$message({
                                    message: '修改失败',
                                    type: 'error'
                                })
                            }
                        })
                        .catch(err => {
                            this.$message({
                                message: '请求异常',
                                type: 'error'
                            })
                        })
                } else {
                    this.$message({
                        message: '请选择咨询师',
                        type: 'warning'
                    })
                    return false;
                }
            });

        },

        //删除
        deleteAppointment(id) {
            this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.$http.delete(`appointment/${id}`)
                    .then(res => {
                        if (res.data.status == 200) {
                            this.$message({
                                type: 'success',
                                message: res.data.message
                            });
                            this.queryList()
                        } else {
                            this.$message({
                                type: 'error',
                                message: '删除失败!'
                            });
                        }

                    })
                    .catch(err => {
                        this.$message({
                            type: 'error',
                            message: '请求异常'
                        });
                    })

            }).catch(() => {
                this.$message({
                    type: 'info',
                    message: '已取消删除'
                });
            });
        },

        //查询咨询预约
        queryList() {
            this.$http.get('appointment/page', { params: this.searchAppointment })
                .then(res => {
                    if (res.data.status == 200) {
                        this.appointmentList = res.data.data.list
                        this.total = res.data.data.total
                    } else {
                        this.$message({
                            message: '数据获取失败',
                            type: 'error'
                        })
                    }
                })
                .catch(err => {
                    this.$message({
                        message: '请求有误',
                        type: 'error'
                    })
                })
        },

        //查询咨询类型
        getTypes() {
            this.$http.get('specialty')
                .then(res => {
                    if (res.data.status == 200) {
                        this.typeList = res.data.data
                    }
                })
                .catch(err => {
                    this.$message({
                        message: '请求异常',
                        type: 'error'
                    })
                })
        },

        //查询所有咨询师
        getConsults() {
            this.$http.get('consult/findAll')
                .then(res => {
                    if (res.data.status == 200) {
                        this.consultList = res.data.data
                    }
                })
                .catch(err => {
                    this.$message({
                        message: '请求异常',
                        type: 'error'
                    })
                })
        },

        //页面尺寸变化
        handleSizeChange(size) {
            this.searchAppointment.pageSize = size
            this.queryList()
        },

        //页面选中
        handleCurrentChange(num) {
            this.searchAppointment.pageNum = num
            this.queryList()
        },

        //点击新增按钮
        clickAdd() {
            this.isadd = true
            this.dialogFormVisible = true
        },
        //清除数据
        clearData() {
            this.appointment = {
                id: null,
                typeId: null,
                content: '',
                consultId: null,
                reply: '',
                status: 0
            }
            this.dialogFormVisible = false
            this.$refs.appointmentForm.resetFields();
        },


    },
    mounted() {
        this.queryList()
        this.getTypes()
        this.getConsults()
    },
    watch: {
        dateList() {
            if (this.dateList != null && this.dateList.length > 0) {
                this.searchAppointment.start = this.dateList[0]
                this.searchAppointment.end = this.dateList[1]
            } else {
                this.searchAppointment.start = null
                this.searchAppointment.end = null
            }
            this.queryList()
        }

    }
}
</script>

<style scoped></style>